package br.com.sanambiental.san.util;

import java.util.Date;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class EmailUtil {

    /**
     * <p>
     * Método responsável por enviar email
     * <p>
     * 
     * @param corpoEmail
     * @author Waltenes Junior
     */
    public void enviarEmail(String corpoEmail) {

	Properties p = new Properties();
	p.put("mail.transport.protocol", "smtp");
	p.put("mail.smtp.host", "smtp.live.com");
	p.put("mail.smtp.socketFactory.port", "587");
	p.put("mail.smtp.socketFactory.fallback", "false");
	p.put("mail.smtp.auth", "true");
	p.put("mail.smtp.port", "587");
	p.put("mail.smtp.starttls.enable", "true");

	Authenticator auth = new Authenticator() {
	    public PasswordAuthentication getPasswordAuthentication() {
		return new PasswordAuthentication("waltenesjr@live.com",
			"%wal1302");
	    }
	};

	Session session = Session.getInstance(p, auth);
	MimeMessage msg = new MimeMessage(session);

	try {

	    msg.setFrom(new InternetAddress("automatico@sanambiental.com.br"));
	    msg.setRecipient(Message.RecipientType.TO, new InternetAddress(
		    "contato@sanambiental.com.br"));
	    msg.setSentDate(new Date());
	    msg.setSubject("[sanambiental.com.br] Mensagem de contato");
	    msg.setContent(corpoEmail, "text/html");

	    Transport.send(msg);

	} catch (AddressException e) {
	    e.getCause();
	} catch (MessagingException e) {
	    e.getCause();
	}
    }
}
